//
//  MiscHelper.m
//  IKnow
//
//

#import "MiscHelper.h"

@implementation MiscHelper


// 获取自1970年以来的秒数
+ (NSUInteger)getUnixTimestamp
{
	return (int) [[NSDate date] timeIntervalSince1970];
}

// 获取某个时间(2010-10-10)的UNIX时间戳
+ (NSUInteger)getUnixTimestamp:(NSString *)dateStr dateFormatter:(NSString *)formatter
{
	NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:formatter];
    NSDate *myDate = [df dateFromString: dateStr];
	
    return (int) [myDate timeIntervalSince1970];
}

// 获取格式化的日期：当前时间
+ (NSString *)getFormatDate:(NSString *)formatter
{
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	[df setDateFormat:formatter];
	
	NSString *date = [df stringFromDate:[NSDate date]];
	
	[df release];
	
	return date;
}

// 获取格式化的日期：指定时间
+ (NSString *)getFormatDate:(NSUInteger)unixTimestamp dateFormatter:(NSString *)formatter
{
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	[df setDateFormat:formatter];
	
	NSString *date = [df stringFromDate:[NSDate dateWithTimeIntervalSince1970:unixTimestamp]];
	
	[df release];
	
	return date;
}

// 获取格式化的日期：将某个日期(20101010)格式化为另一个格式的日期(2010年10月10日)
+ (NSString *)getFormatDate:(NSString *)dateStr fromDateFormatter:(NSString *)from toDateFormatter:(NSString *)to
{
	NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:from];
    NSDate *myDate = [df dateFromString: dateStr];
	
	
	NSDateFormatter *dfx = [[NSDateFormatter alloc] init];
	[dfx setDateFormat:to];
	
	NSString *date = [dfx stringFromDate:myDate];
	
	[dfx release];
	
	return date;
}

+ (NSString *) formattedDateRelativeToNow:(NSDate *)date
{
	NSDateFormatter *mdf = [[NSDateFormatter alloc] init];
	[mdf setDateFormat:@"yyyy-MM-dd"];
	NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:date]];
	[mdf release];
	
	NSInteger dayDiff = (int)[midnight timeIntervalSinceNow] / (60*60*24);	
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];	
	
	if(dayDiff == 0) 
		[dateFormatter setDateFormat:@"'Today, 'h':'mm aaa"];
	else if(dayDiff == -1) 
		[dateFormatter setDateFormat:@"'Yesterday, 'h':'mm aaa"];
	else if(dayDiff == -2) 
		[dateFormatter setDateFormat:@"MMMM d', Two days ago'"]; 	
	else if(dayDiff > -7 && dayDiff <= -2) 
		[dateFormatter setDateFormat:@"MMMM d', This week'"]; 
	else if(dayDiff > -14 && dayDiff <= -7) 
		[dateFormatter setDateFormat:@"MMMM d'; Last week'"]; 
	else if(dayDiff >= -60 && dayDiff <= -30) 
		[dateFormatter setDateFormat:@"MMMM d'; Last month'"]; 
	else if(dayDiff >= -90 && dayDiff <= -60) 
		[dateFormatter setDateFormat:@"MMMM d'; Within last three months'"]; 
	else if(dayDiff >= -180 && dayDiff <= -90) 
		[dateFormatter setDateFormat:@"MMMM d'; Within last six months'"]; 
	else if(dayDiff >= -365 && dayDiff <= -180) 
		[dateFormatter setDateFormat:@"MMMM d, YYYY'; Within this year'"]; 
	else if(dayDiff < -365) 
		[dateFormatter setDateFormat:@"MMMM d, YYYY'; A long time ago'"];
	
	return [dateFormatter stringFromDate:date];
}
@end
